"""
@author: 江同学呀
@file:  bulk.py
@date: 2025/3/26 22:17
@desc: 
    
"""
from espc import ESPrivyCouncil
from espc.common.field_common import TimeFormat
from espc.orm.api.document_api.bulk_api import BulkApi
from espc.orm.model.index_module.index_setting import IndexSetting
from espc.orm.model.mapping.field.common.dates_field import DateField
from espc.orm.model.mapping.field.common.keyword_field import KeywordField
from espc.orm.model.mapping.field.common.numeric_field import LongField, ShortField
from espc.orm.model.mapping.field.object.nested_field import NestedField
from espc.orm.model.mapping.field.text_search.text_field import TextField
from espc.orm.model.model.base_alias import Alias
from espc.orm.model.model.base_document import BaseDocument
from espc.orm.model.model.base_index import BaseIndex


class TestDocument(BaseDocument):
    """

    """
    file_id: LongField = LongField()
    title: TextField = TextField(
        analyzer="ik_max_word",
        fields=[
            KeywordField(field_name="keyword", ignore_above=10922),
            TextField(field_name="en", analyzer="english"),
            TextField(field_name="cn", analyzer="ik_max_word"),
        ]
    )
    context: TextField = TextField(
        analyzer="ik_max_word",
        fields=[
            KeywordField(field_name="keyword", ignore_above=10922),
            TextField(field_name="en", analyzer="english"),
            TextField(field_name="cn", analyzer="ik_max_word"),
        ]
    )
    page_num: ShortField = ShortField()
    metadata: NestedField = NestedField()
    date: DateField = DateField(format_=[TimeFormat.yyyy_mm_dd_hh_mm_ss, TimeFormat.yyyy_mm_dd, TimeFormat.epoch_millis])


class TestIndex(BaseIndex):
    index_name: str = "test"
    alias: Alias = Alias(alias_name="test")
    settings: IndexSetting = IndexSetting(
        number_of_shards=3,
        number_of_replicas=1,
        analysis={
            "normalizer": {
                "lowercase": {
                    "type": "custom",
                    "filter": ["lowercase"],
                }
            }
        }
    )
    document = TestDocument

if __name__ == "__main__":
    espc = ESPrivyCouncil()
    espc.init_es(
        hosts="http://127.0.0.1:9200",
        sniff_on_start=True,
        # es集群服务器结点连接异常时是否刷新es节点信息
        sniff_on_connection_fail=True,
        # 每60秒刷新节点信息
        sniffer_timeout=60,
        timeout=30,
        max_retries=10,
        retry_on_timeout=True,
    )
    espc.create_index(TestIndex)

    bulk_api = BulkApi()
    bulk_api = bulk_api.docs(
        docs=[
            TestDocument(file_id=i + 1, title="test", context="aaaaaaaa", page_num=1, date="2025-03-26")
            for i in range(100)
        ],
        index=TestIndex,
    )
    bulk_result = bulk_api.post()
    pass